<script setup lang="ts">
import { ref } from 'vue'
import { loginApi } from '@/services/auth'
import { useMemberStore } from '@/stores'

const memberStore = useMemberStore()
const phone = ref('')
const password = ref('')

async function login() {
  uni.showToast({
    title: '登录中...',
  })

  const userInfo = await loginApi(phone.value, password.value)
  memberStore.setProfile(userInfo)

  await uni.showToast({
    title: '登录成功', duration: 2000,
  })
  setTimeout(() => {
    const pages = getCurrentPages()
    if ('pages/auth/register' === pages[pages.length - 2].route) {
      uni.switchTab({ url: '/pages/index/index' })
    } else {
      uni.navigateBack()
    }
  }, 2000)
}

function goRegister() {
  uni.navigateTo({
    url: '/pages/auth/register',
  })
}
</script>

<template>
  <view class="abslrtb flex-column a-center wrap">
    <view class="topbox flex-column aj-center">
      <!--<image class="logoimg" src="/static/images/bubble.png"></image>-->
    </view>

    <view class="form">
      <view class="flex a-center form-item">
        <view class="label">
          <text>手机号</text>
        </view>
        <image class="label_icon" src="/static/images/icon_user.png"></image>
        <view class="label_fgs"></view>
        <view class="flex-1">
          <input placeholder-class="placeholder" class="qui-input" type="text" v-model="phone"
                 placeholder="请输入手机号" />
        </view>
      </view>
      <view class="flex a-center form-item">
        <view class="label">
          <text>密码</text>
        </view>
        <image class="label_icon" src="/static/images/icon_pw.png"></image>
        <view class="label_fgs"></view>
        <view class="flex-1">
          <input placeholder-class="placeholder" :password="true" class="qui-input" type="text" v-model="password"
                 placeholder="请输入密码" />
        </view>
      </view>
    </view>

    <view class="btns">
      <view class="qbtn" @click="login">
        <text class="btn-text-color fs30">登录</text>
      </view>
      <view class="flex ptb30 mlr20 space-between">
        <view @click="goRegister" class="">
          <text class="fs26 nav-text-color underline">注册</text>
        </view>
      </view>
    </view>

  </view>
</template>

<style lang="scss">
//
</style>
